<?php

/**
 * Alquiler form base class.
 *
 * @method Alquiler getObject() Returns the current form's model object
 *
 * @package    blackDrums
 * @subpackage form
 * @author     your name here
 */
abstract class BaseAlquilerForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'idAlquiler'        => new sfWidgetFormInputHidden(),
      'horaInicio'        => new sfWidgetFormTime(),
      'horaFin'           => new sfWidgetFormTime(),
      'direccion'         => new sfWidgetFormInputText(),
      'fecha'             => new sfWidgetFormDate(),
      'idClienteNatural'  => new sfWidgetFormPropelChoice(array('model' => 'ClienteNatural', 'add_empty' => true)),
      'idClienteJuridico' => new sfWidgetFormPropelChoice(array('model' => 'ClienteJuridico', 'add_empty' => true)),
      'idEvento'          => new sfWidgetFormPropelChoice(array('model' => 'Evento', 'add_empty' => false)),
      'detalle_list'      => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Equipo')),
    ));

    $this->setValidators(array(
      'idAlquiler'        => new sfValidatorPropelChoice(array('model' => 'Alquiler', 'column' => 'idAlquiler', 'required' => false)),
      'horaInicio'        => new sfValidatorTime(),
      'horaFin'           => new sfValidatorTime(),
      'direccion'         => new sfValidatorString(array('max_length' => 150)),
      'fecha'             => new sfValidatorDate(),
      'idClienteNatural'  => new sfValidatorPropelChoice(array('model' => 'ClienteNatural', 'column' => 'cedula', 'required' => false)),
      'idClienteJuridico' => new sfValidatorPropelChoice(array('model' => 'ClienteJuridico', 'column' => 'rif', 'required' => false)),
      'idEvento'          => new sfValidatorPropelChoice(array('model' => 'Evento', 'column' => 'idEvento')),
      'detalle_list'      => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Equipo', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('alquiler[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Alquiler';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['detalle_list']))
    {
      $values = array();
      foreach ($this->object->getDetalles() as $obj)
      {
        $values[] = $obj->getIdequipo();
      }

      $this->setDefault('detalle_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveDetalleList($con);
  }

  public function saveDetalleList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detalle_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetallePeer::IDALQUILER, $this->object->getPrimaryKey());
    DetallePeer::doDelete($c, $con);

    $values = $this->getValue('detalle_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Detalle();
        $obj->setIdalquiler($this->object->getPrimaryKey());
        $obj->setIdequipo($value);
        $obj->save();
      }
    }
  }

}
